package SVN::TeamTools::Store::DepIndex;
use parent 'SVN::TeamTools::Index::Index';

use warnings;
use strict;
use Carp;
use Error qw(:try);

use Sys::Hostname qw( hostname );

use SVN::TeamTools::Store::Config;
use SVN::TeamTools::Store::Repo;
use HTML::Template;

my $conf;
my $logger;
BEGIN { $conf = SVN::TeamTools::Store::Config->new(); $logger = $conf->{logger}; }

sub hasAction {
        shift;
        my %args        = @_;
        my $action      = $args{action};
        return ("|dsp.config|" =~ /\|\Q$action\E\|/);
}

sub getTemplate {
	return HTML::Template->new( filename => 'SVN/TeamTools/Store/tmpl/depindex-config.tmpl', path => @INC );
}

# #########################################################################################################
#
# Lucy functions
#

# Constructor:
#   - create (0/1)
sub new {
	my $class	= shift;
	my %args	= @_;
	my $mode	= $args{mode};
	my $create	= $args{create};

	my $self = $class->SUPER::new (path => $conf->{depindex}, mode => $mode, schema => _getSchema(), create => $create);

	bless  $self, $class;
        if ($create == 1) {
                $self->setIndexRev(rev => 0);
        }

	return $self;
}

sub addDep {
	my $self	= shift;
	my %args	= @_;
	my $module	= $args{module};
	my $path	= $args{path};

	try {
		$self->getWriter()->add_doc (Lucy::Document::Doc->new(fields => {
			type	=> 'rel',
			module	=> $module,
			path	=> $path,
		}));
        } otherwise {
                my $exc = shift;
                croak "Error writing dependency object, error: $exc";
        };
}
sub delDepModule {
	my $self	= shift;
	my %args	= @_;
	my $module	= $args{module};

	$self->delTerm (field => 'module', term => $module);
}
sub delDepPath {
	my $self	= shift;
	my %args	= @_;
	my $path	= $args{path};

	$self->delTerm (field => 'path', term => $path);
}
###################################################################################################
### Create Lucy Index
#
# Static method
sub _getSchema {
	my $schema = Lucy::Plan::Schema->new;
	my $rawtype = Lucy::Plan::StringType->new(indexed=>1, stored=>1, sortable=>0, boost=>1.0);

	$schema->spec_field( name => 'type', 	type => $rawtype ); # 'status' or 'rel'
	$schema->spec_field( name => 'rev', 	type => $rawtype ); # Only used if status
	$schema->spec_field( name => 'module', 	type => $rawtype ); # The module referenced by the path
	$schema->spec_field( name => 'path', 	type => $rawtype ); # The path pointing to the doc that contains the module

	return $schema;
}
1;
